package myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Base64;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import myapp.MyApp;
import util.MyLog;
import util.RequestManager;

public class PassCodeImageView extends AppCompatImageView {
    public PassCodeImageView(Context context) {
        this(context, null);
    }

    public PassCodeImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PassCodeImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        getCode();
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                getCode();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String s = (String) msg.obj;
                    break;

            }
        }
    };

    public void getCode() {
        MyApp myApp = (MyApp) getContext().getApplicationContext();
        String url = myApp.getWebConfig() + "/api/sms/getCaptcha";
        HashMap<String, String> paramsMap = new HashMap<>();
        RequestManager.getInstance(getContext()).requestAsyn(url, RequestManager.TYPE_POST_FORM, paramsMap, new RequestManager.ReqCallBack() {
            @Override
            public void onReqSuccess(Object result) {
                MyLog.e("code--result", result.toString());
//                {"code":1,"msg":"发送成功","time":"1571105679","data":8851}
                Message msg = new Message();
                try {
                    JSONObject jsonObject = new JSONObject(result.toString());
                    if (jsonObject.getString("code").equals("1")) {
                        JSONObject imgStr = jsonObject.optJSONObject("data");
                        String img = imgStr.optString("imgStr").replace("data:image/png;base64,", "");
                        byte[] bitmapArray = Base64.decode(img, Base64.DEFAULT);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
                        setImageBitmap(bitmap);
//                        msg.what = 1;
//                        msg.obj = img;
//                        handler.sendMessage(msg);
                        return;
                    }
                    msg.arg1 = 2;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                MyLog.e("result", errorMsg);
                if (errorMsg.equals("未登录")) {
                    Message msg = new Message();
                    msg.arg1 = 11;
                    handler.sendMessage(msg);
                }
            }
        });
    }
}
